/**
 * 
 */
package hu.radai.jsfportal.log;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * @author radai.tamas
 */
public class LogManagerUtil implements LogManager {

	private static Logger logger;

	public static void init(final String configFileName) {
		try {
			if (configFileName == null || configFileName.isEmpty())
				throw new NullPointerException();
			DOMConfigurator.configure(configFileName);
			debug(LogManagerUtil.class, "Log4j init done ...");
		} catch (final Throwable t) {
			t.printStackTrace();
		}
	}

	/**
	 * Ha az {@code assertion} feltetel erteke:
	 * <ul>
	 * <li>{@code false}, akkor {@code ERROR} szinten {@code message} uzenetet
	 * naploz</li>
	 * <li>{@code true}, akkor nem csinal semmit.</li>
	 * </ul>
	 * 
	 * @param assertion
	 *            feltetel
	 * @param message
	 *            uzenet
	 */
	public static void assertLog(final Boolean assertion, final String message) {
		logger.assertLog(assertion, message);
	}

	/**
	 * Altalanos naplozo kivetellel.
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param level
	 *            naplozas szintje
	 * @param message
	 *            uzenet
	 * @param t
	 *            kivetel
	 * @see Logger#log(Priority, Object, Throwable)
	 */
	public static void log(final Class<?> clazz, final Level level,
			final String message, final Throwable t) {
		logger = Logger.getLogger(clazz);
		if (t != null)
			logger.log(level, message, t);
		else
			logger.log(level, message);
	}

	/**
	 * Altalanos naplozo kivetel nelkul.
	 * 
	 * @param clazz
	 *            nalozo osztaly
	 * @param level
	 *            naplozas szintje
	 * @param message
	 *            uzenet
	 * @see LogManagerUtil#log(Class, Level, String, Throwable)
	 */
	public static void log(final Class<?> clazz, final Level level,
			final String message) {
		log(clazz, level, message, null);
	}

	/**
	 * {@code TRACE} szinten naplozo kivetellel. ({@code TRACE_INT = 5000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 * @param t
	 *            kivetel
	 */
	public static void trace(final Class<?> clazz, final String message,
			final Throwable t) {
		logger = Logger.getLogger(clazz);
		if (t != null)
			logger.trace(message, t);
		else
			logger.trace(message);
	}

	/**
	 * {@code TRACE} szinten naplozo kivetel nelkul. ({@code TRACE_INT = 5000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 */
	public static void trace(final Class<?> clazz, final String message) {
		trace(clazz, message, null);
	}

	/**
	 * {@code DEBUG} szinten naplozo kivetellel. ({@code DEBUG_INT = 10000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 * @param t
	 *            kivetel
	 */
	public static void debug(final Class<?> clazz, final String message,
			final Throwable t) {
		logger = Logger.getLogger(clazz);
		if (t != null)
			logger.debug(message, t);
		else
			logger.debug(message);
	}

	/**
	 * {@code DEBUG} szinten naplozo kivetel nelkul. ({@code DEBUG_INT = 10000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 */
	public static void debug(final Class<?> clazz, final String message) {
		debug(clazz, message, null);
	}

	/**
	 * {@code INFO} szinten naplozo kivetellel. ({@code INFO_INT = 20000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 * @param t
	 *            kivetel
	 */
	public static void info(final Class<?> clazz, final String message,
			final Throwable t) {
		logger = Logger.getLogger(clazz);
		if (t != null)
			logger.info(message, t);
		else
			logger.info(message);
	}

	/**
	 * {@code INFO} szinten naplozo kivetel nelkul. ({@code INFO_INT = 20000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 */
	public static void info(final Class<?> clazz, final String message) {
		info(clazz, message, null);
	}

	/**
	 * {@code WARN} szinten naplozo kivetellel. ({@code WARN_INT = 30000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 * @param t
	 *            kivetel
	 */
	public static void warn(final Class<?> clazz, final String message,
			final Throwable t) {
		logger = Logger.getLogger(clazz);
		if (t != null)
			logger.warn(message, t);
		else
			logger.warn(message);
	}

	/**
	 * {@code WARN} szinten naplozo kivetel nelkul. ({@code WARN_INT = 30000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 */
	public static void warn(final Class<?> clazz, final String message) {
		warn(clazz, message, null);
	}

	/**
	 * {@code ERROR} szinten naplozo kivetellel. ({@code ERROR_INT = 40000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 * @param t
	 *            kivetel
	 */
	public static void error(final Class<?> clazz, final String message,
			final Throwable t) {
		logger = Logger.getLogger(clazz);
		if (t != null)
			logger.error(message, t);
		else
			logger.error(message);
	}

	/**
	 * {@code ERROR} szinten naplozo kivetel nelkul. ({@code ERROR_INT = 40000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 */
	public static void error(final Class<?> clazz, final String message) {
		error(clazz, message, null);
	}

	/**
	 * {@code FATAL} szinten naplozo kivetellel. ({@code FATAL_INT = 50000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 * @param t
	 *            kivetel
	 */
	public static void fatal(final Class<?> clazz, final String message,
			final Throwable t) {
		logger = Logger.getLogger(clazz);
		if (t != null)
			logger.fatal(message, t);
		else
			logger.fatal(message);
	}

	/**
	 * {@code FATAL} szinten naplozo kivetel nelkul. ({@code FATAL_INT = 50000})
	 * 
	 * @param clazz
	 *            naplozo osztaly
	 * @param message
	 *            uzenet
	 */
	public static void fatal(final Class<?> clazz, final String message) {
		fatal(clazz, message, null);
	}
}
